package cn.stylefeng.guns.modular.gridsystem.listener;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.modular.gridsystem.model.param.GatewayImportParam;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.regex.Pattern;

/**
 * 网关导入导入监听类
 *
 * @author zxw 2022/11/15 14:02
 */
public class GatewayImportListener extends AnalysisEventListener<GatewayImportParam> {

    @Override
    public void invoke(GatewayImportParam param, AnalysisContext analysisContext) {

        // 网关名称
        if (StrUtil.isBlank(param.getGatewayName())) {
            throw new ServiceException(StrFormatter.format("序号：{}的网关名称没有填写", param.getSeqNo()));
        }

        // 网关编码
        if (StrUtil.isBlank(param.getGatewayCode())) {
            throw new ServiceException(StrFormatter.format("序号：{}的网关编码没有填写", param.getSeqNo()));
        }

        // 通道数量
        if (ObjectUtil.isEmpty(param.getChannelNum())) {
            throw new ServiceException(StrFormatter.format("序号：{}的通道数量没有填写", param.getSeqNo()));
        }

        Pattern pattern = Pattern.compile("[1-9]+[0-9]*");
        if (!pattern.matcher(param.getChannelNum()).matches()) {
            throw new ServiceException(StrFormatter.format("序号：{}的通道数量必须为大于0的整数", param.getSeqNo()));
        }

        // 阀值
        if (ObjectUtil.isEmpty(param.getThreshold())) {
            throw new ServiceException(StrFormatter.format("序号：{}的阀值没有填写", param.getSeqNo()));
        }

        if (!pattern.matcher(param.getThreshold()).matches()) {
            throw new ServiceException(StrFormatter.format("序号：{}阀值必须为大于0的整数", param.getSeqNo()));
        }

        // 网关类型
        if (ObjectUtil.isEmpty(param.getTypeName())) {
            throw new ServiceException(StrFormatter.format("序号：{}的网关类型没有选择", param.getSeqNo()));
        }

        // 配置场景
        if (ObjectUtil.isEmpty(param.getSceneName())) {
            throw new ServiceException(StrFormatter.format("序号：{}的配置场景没有选择", param.getSeqNo()));
        }

        // 传感器类型
        if (ObjectUtil.isEmpty(param.getSensorType())) {
            throw new ServiceException(StrFormatter.format("序号：{}的传感器类型没有选择", param.getSeqNo()));
        }

        if (!param.getSensorType().contains("|")){
            throw new ServiceException(StrFormatter.format("序号：{}的传感器类型数据不正确", param.getSeqNo()));
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
